<template>
  <el-dialog
    :title="!dataForm.maId ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="地址" prop="maAddress">
      <el-input v-model="dataForm.maAddress" placeholder="地址"></el-input>
    </el-form-item>
    <el-form-item label="详细地址" prop="maAddressDetailed">
      <el-input v-model="dataForm.maAddressDetailed" placeholder="详细地址"></el-input>
    </el-form-item>
    <el-form-item label="联系昵称" prop="maConnectName">
      <el-input v-model="dataForm.maConnectName" placeholder="联系昵称"></el-input>
    </el-form-item>
    <el-form-item label="联系电话" prop="maConnectMobile">
      <el-input v-model="dataForm.maConnectMobile" placeholder="联系电话"></el-input>
    </el-form-item>
    <el-form-item label="用户号" prop="maUsername">
      <el-input v-model="dataForm.maUsername" placeholder="用户号"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          maId: 0,
          maAddress: '',
          maAddressDetailed: '',
          maConnectName: '',
          maConnectMobile: '',
          maUsername: ''
        },
        dataRule: {
          maAddress: [
            { required: true, message: '地址不能为空', trigger: 'blur' }
          ],
          maAddressDetailed: [
            { required: true, message: '详细地址不能为空', trigger: 'blur' }
          ],
          maConnectName: [
            { required: true, message: '联系昵称不能为空', trigger: 'blur' }
          ],
          maConnectMobile: [
            { required: true, message: '联系电话不能为空', trigger: 'blur' }
          ],
          maUsername: [
            { required: true, message: '用户号不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.maId = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.maId) {
            this.$http({
              url: this.$http.adornUrl(`/sulinguser/memberaddress/info/${this.dataForm.maId}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.maAddress = data.memberAddress.maAddress
                this.dataForm.maAddressDetailed = data.memberAddress.maAddressDetailed
                this.dataForm.maConnectName = data.memberAddress.maConnectName
                this.dataForm.maConnectMobile = data.memberAddress.maConnectMobile
                this.dataForm.maUsername = data.memberAddress.maUsername
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/sulinguser/memberaddress/${!this.dataForm.maId ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'maId': this.dataForm.maId || undefined,
                'maAddress': this.dataForm.maAddress,
                'maAddressDetailed': this.dataForm.maAddressDetailed,
                'maConnectName': this.dataForm.maConnectName,
                'maConnectMobile': this.dataForm.maConnectMobile,
                'maUsername': this.dataForm.maUsername
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
